// pages/user/user.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['男', '女'],
    user: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({ user: app.User });
    app.Login(function () {
      that.setData({ user: app.User });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var user=that.data.user;
        user.HeadImg = res.tempFilePaths[0];
        that.setData({ user: user });
      }
    })
  },
  openSex:function(e){
    var index = e.detail.value;
    var user = this.data.user;
    user.Sex = this.data.array[index];
    this.setData({ user:user });
  },
  openRealName: function (e){
    var realname = e.detail.value;
    var user = this.data.user;
    user.RealName = realname;
    //this.setData({ user: user });
  },
  Save:function(){
    var user = this.data.user;
    if (user.RealName.trim() == "") {
      app.showError("请输入用户名");
      return;
    }
    wx.showLoading({ title: '正在保存...' });
    if(user.HeadImg!=app.User.HeadImg){
    wx.uploadFile({
      url: 'https://company.qiyedaishu.com/tools/Upload.ashx', //仅为示例，非真实的接口地址
      filePath: user.HeadImg,
      name: 'img',
      formData: {
        'handle': 'upload_file_t'
      },
      success: function (res) {
        var result = res.data.split('*');
        var data = {};
        data.act = "Edit_User";
        data.headimg=result[0];
        data.realname=user.RealName;
        data.Sex=user.Sex;
        app.Ajax(data, function (json) {
          wx.hideLoading();
          if (json.state) {
            app.showSuccess("保存成功");
            app.User=user;
            setTimeout(function () {
              wx.navigateBack();
            }, 1000);
          }else{
            app.showError("保存失败");
          }
        });
      }
    });
    }else{
      var data = {};
      data.act = "Edit_User";
      data.headimg = user.HeadImg;
      data.realname = user.RealName;
      data.Sex = user.Sex;
      app.Ajax(data, function (json) {
        wx.hideLoading();
        if (json.state) {
          app.showSuccess("保存成功");
          app.User = user;
          setTimeout(function () {
            wx.navigateBack();
          }, 1000);
        } else {
          app.showError("保存失败");
        }
      });
    }
  }
})